package com.ixuestar.controller;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ixuestar.service.EasyPrintingService;
import com.ixuestar.util.ReturnResult;
import com.ixuestar.wxutil.GetwxOpenId;
import com.ixuestar.wxutil.PayConfigUtil;

public class EasyPrintingController extends BaseController{

	
	
	@Autowired
	private EasyPrintingService easyPrintingService;
	/**
	 * @method 获取sessionKey
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getWxSessionKey")
	public Map<String, Object> getWxSessionKey(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//创建返回参数
		Map<String, Object> map = null;
		try {
			String appId = PayConfigUtil.ixuestarwx_APP_ID_Y;
			String appSec = PayConfigUtil.ixuestarwx_APP_SEC_Y;
			System.out.println("-----------进入getWxSessionKey接口-------------");
			String code = jsonObject.getString("code");
			//执行接口
			JSONObject wxSessionKey = GetwxOpenId.getWxSessionKey(code,appId,appSec);
			//
			map = ReturnResult.correctResult(wxSessionKey);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 通过手机号信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping("/getWxuserInfo")
	public Map<String, Object> getWxTelephone(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//创建返回参数
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		System.out.println("-----------进入getWxTelephone接口-------------");
		//请求接口
		JSONObject wxuserInfo = easyPrintingService.getWxuserInfo(jsonObject);
		Integer code = wxuserInfo.getInteger("code");
		JSONObject yxWxuser = wxuserInfo.getJSONObject("yxWxuser");
		if (code==0) {
			yxWxuser.remove("code");
			map = ReturnResult.otherJSONObjectResult(1, yxWxuser, "用户的第一次登陆");
		}else if (code==1) {
			map = ReturnResult.otherResult(3, "未获取到用户的手机对象");
		}else if (code==3) {
			yxWxuser.remove("code");
			map = ReturnResult.correctResult(yxWxuser);
		}
		return map;
	}
}
